/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test_system.menu;

import java.util.Scanner;
import test_system.controller.TestController;
import test_system.questions.TestWrapper;
import test_system.user_logic.Session;
import test_system.user_logic.SessionSaver;

/**
 *
 * @author root
 */
public class Menu {

    private Scanner in = new Scanner(System.in);
    Language language;
    Session session;

    public Menu(TestWrapper test, Session session) {
        this.session = session;
    }

    public Session avtorization() {
        Session session;
        int tryCounter = 0;
        do {
            if (tryCounter == 3) {
                System.out.println("The programm will close.");
                System.exit(0);
            }
            System.out.println("Авторизація:");
            System.out.println("Логін:");
            String login = in.nextLine();
            System.out.println("Пароль:");
            String password = in.nextLine();
            session = Session.createSession(login, password);
            tryCounter++;
        } while (session == null);
        return session;
    }

    public String[] runTestDialog() {
        checkLanguage();
        System.out.println(TestController.test);
        String[] result = new String[TestController.test.getTestLength()];
        for (int i = 0; i < TestController.test.getTestLength(); i++) {
            outputQuestion(i);
            String ans = in.nextLine();
            if(ans.equals("s")){
                SessionSaver ss = new SessionSaver(TestController.userSession.getUser(), TestController.test, result);
                ss.save();
            }
            result[i] = ans;
        }
        return result;
    }

    public String[] continuePooling(String[] answers, int index) {
        checkLanguage();
        for (int i = index; i < TestController.test.getTestLength(); i++) {
            outputQuestion(i);
            System.out.print(i);
            String ans = in.nextLine();
            if(ans.equals("s")){
                SessionSaver ss = new SessionSaver(session.getUser(), TestController.test, answers);
                ss.save();
            }
            answers[i] = ans;
        }
        return answers;
    }

    public void markOutput(boolean[] userResult) {
        int mark = 0;
        for (int i = 0; i < userResult.length; i++) {
            if (userResult[i]) {
                mark++;
            }
        }
        String markOutout = "%1$s %2$d/%3$d";
        System.out.println(String.format(markOutout, language.markOutout(), mark, userResult.length));
    }


    private void outputQuestion(int index) {
        String[] languageContext = language.getQuestionContext();
        System.out.print("To save and exit press \"s\". ");
        String fullQuestion = "%1$s\n%2$s";
        if (TestController.test.getQuestion(index).getClassName().equals("TextAnswerQuestion")) {
            System.out.println(String.format(fullQuestion, languageContext[0], TestController.test.getQuestion(index).toString()));
        } else {
            System.out.println(String.format(fullQuestion, languageContext[1], TestController.test.getQuestion(index).toString()));
        }

    }

    private void checkLanguage() {
        String lan;
        do {
            System.out.println("Please, check your language:\n1)English\n2)Українська\n3)Русский");
            lan = in.nextLine();
            if (lan.equals("1")) {
                language = new EnLanguage();
            } else if (lan.equals("2")) {
                language = new UaLanguage();
            } else if (lan.equals("3")) {
                language = new RuLanguage();
            } else {
                System.out.println("Check right answer.");
            }
        } while (!(lan.equals("1") || lan.equals("2") || lan.equals("3")));
    }
}
